import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:dynamic_stock/const/const.dart';
import 'package:dynamic_stock/model/car_list_mode.dart';
import 'package:dynamic_stock/model/company_model.dart';
import 'package:dynamic_stock/model/company_user_model.dart';
import 'package:dynamic_stock/model/home_company_percent.dart';
import 'package:dynamic_stock/model/home_model.dart';
import 'package:dynamic_stock/model/login_model.dart';
import 'package:http/http.dart' as http;
import 'package:package_info/package_info.dart';
import 'package:shared_preferences/shared_preferences.dart';

const _KCM_PARTNER = "3000008";
const _KCM_PARTNER_KEY = "9f16ebfc283ecbf3c016c55d87e206cc";

const _API_ROOT = "https://szyrwl.com/test/api/";

const _api_login = "api/app/saasuser/loginSaasUser.do";
const _api_home = "api/cms/statistics/dynamicStock/getStatisticsDynamicStockDatas.do"; // 获取公司列表
const _api_company_list = "api/app/saassteward/getStewardComListByPage.do";
const _api_company_user_list = "api/app/saassteward/getStewardUserListByPage.do";
const _api_car_list = "api/app/saassteward/getSaasVehicleListByPage.do";
const _api_home_company_percent = "api/cms/statistics/dynamicStock/getLastMonthDynamicStockByComId.do";// 获取公司当月分红


// 测试用户// caotuo
// 111111

class NetworkTool {

 String _sign(String sender) {
    String strSign = "partner=$_KCM_PARTNER&appkey=$_KCM_PARTNER_KEY&data=$sender";
    return _generateMd5(strSign);
  }

  // md5 加密
 String _generateMd5(String data) {
    var content = new Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    return digest.toString();
  }

 Map _createRequestParam(Map params, String version, [String loginuserid]) {
    String jsonstr = jsonEncode(params);
    String res = _sign(jsonstr);
    var ressender = {"sign":res, "data":jsonstr, "partner":_KCM_PARTNER,"clientVersion": version};
    if(loginuserid != null) ressender['loginUserId'] = loginuserid;
    return ressender;
  }



  //----------登录请求
  Future<LoginModel> request_login(Map params) async {
    var res = await _post(_api_login, params);
    return LoginModel.fromJson(res);
  }

  //----------首页数据请求
  Future<HomeModel> request_home(Map params) async {

    var res = await _post(_api_home, params);
    return HomeModel.fromJson(res);

  }

  //----------请求车管家公司列表
  Future<CompanyListModel> request_company(Map params) async {

    var res = await _post(_api_company_list, params);
    return CompanyListModel.fromJson(res);

  }

  //----------请求车管家公司用户列表
  Future<CompanyUserModelData> request_company_user(Map params) async {

    var res = await _post(_api_company_user_list, params);
    return CompanyUserModelData.fromJson(res);

  }

  //----------请求车辆列表
  Future<CarListModel> request_car_list(Map params) async {
    
    var res = await _post(_api_car_list, params);
    return CarListModel.fromJson(res);

  }


  //----------请求公司当月分红
  Future<HomePercentModel> request_company_mouth(Map params) async {
    
    var res = await _post(_api_home_company_percent, params);
    return HomePercentModel.fromJson(res);

  }

  Future<dynamic> _post(String url, Map params) async {
    
    PackageInfo res = await PackageInfo.fromPlatform();
    SharedPreferences share = await SharedPreferences.getInstance();
    String loginuserId = share.getString(USER_LOGIN_ID);
    
    final response = await http.post(_API_ROOT + url, body:_createRequestParam(params, res.version, loginuserId));
    if (response.statusCode == 200) {
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return result;
    } else {
      throw Exception("网络请求失败");
    }
  }
}